package com.doyens.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.StudentDto;
import com.doyens.entity.*;
import com.doyens.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/student")
@Api(tags = "学生管理接口")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private RecruiterService recruiterService;

    @Autowired
    private ClassTeacherService classTeacherService;

    @Autowired
    private IntegralService integralService;

    @Autowired
    private DownnumService downnumService;


    /**
     * 学生信息分页查询
     *
     * @param page     当前查询页码
     * @param pageSize 每页展示记录数
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询学生信息方法")
    public R<Page> page(int page, int pageSize,String idcard, String sname) {

        log.info("page={},pageSize={}", page, pageSize);

        //构造分页构造器对象
        Page<Student> pageInfo = new Page<>(page, pageSize);
        Page<StudentDto> studentDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //封装模糊查询条件
        queryWrapper.like(sname != null, Student::getSname, sname);
        //封装查询条件，按照身份证号查询学生
        queryWrapper.eq(idcard != null,Student::getIdcard,idcard);
        //添加排序条件
        queryWrapper.orderByAsc(Student::getUpdateTime);
        //执行查询
        studentService.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, studentDtoPage, "records");
        List<Student> records = pageInfo.getRecords();
        List<StudentDto> list = records.stream().map((item) -> {

            StudentDto studentDto = new StudentDto();
            BeanUtils.copyProperties(item, studentDto);
            //获取招生老师id
            int recruiterId = item.getRecruiterId();
            //根据id查询招生老师对象
            Recruiter recruiter = recruiterService.getById(recruiterId);

            //判断对象
            if (recruiter != null) {
                String recruiterName = recruiter.getRecruiterName();
                studentDto.setRecruiterName(recruiterName);
            }

            //获取班主任id
            int teacherId = item.getClassTeacherId();
            //根据id查询招生老师对象
            ClassTeacher classTeacher = classTeacherService.getById(teacherId);

            if (classTeacher != null) {
                String classTeacherName = classTeacher.getClassteacherName();
                studentDto.setClassTeacherName(classTeacherName);
            }

            //获取综合积分id
            int integralId = item.getStudentId();
            //根据id查询招生老师对象
            Integral integral = integralService.getById(integralId);

            if (integral != null) {
                Integer score = integral.getScore();
                studentDto.setScore(score);
            }

            //获取末班次数id
            int downnumId = item.getStudentId();
            Downnum downnum = downnumService.getById(downnumId);

            if (downnum != null) {
                String numStaydown = downnum.getNumStaydown();
                studentDto.setNumStaydown(numStaydown);
            }

            return studentDto;
        }).collect(Collectors.toList());
        studentDtoPage.setRecords(list);

        return R.success(studentDtoPage);
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加学生方法")
    public R<String> save(Student student) {
        log.info(student.toString());

        studentService.save(student);

        return R.success("新增学生信息成功");
    }

    /**
     * 根据id修改员工信息
     *
     * @param student
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改学生信息方法")
    public R<String> update(Student student) {
        log.info(student.toString());
        boolean b = studentService.updateById(student);
        if (b) {
            return R.success("学生信息修改成功!");
        }
        return R.error("修改信息失败");
    }

    /**
     * 根据id查询学生信息
     *
     * @param studentId
     * @return
     */
    @GetMapping("{studentId}")
    @ApiOperation(value = "根据id查询学生信息方法")
    public R<StudentDto> getById(@PathVariable String studentId) {
        log.info("根据id查询员工信息");
        Student student = studentService.getById(studentId);
        StudentDto studentDto = new StudentDto();
        BeanUtils.copyProperties(student,studentDto);
        //获取班主任姓名
        Integer classTeacherId = student.getClassTeacherId();
        ClassTeacher classTeacher = classTeacherService.getById(classTeacherId);
        if (classTeacher!=null){
            String classteacherName = classTeacher.getClassteacherName();
            studentDto.setClassTeacherName(classteacherName);
        }
        //获取招生老师姓名
        Integer recruiterId = student.getRecruiterId();
        Recruiter recruiter = recruiterService.getById(recruiterId);
        if (recruiter!=null){
            String recruiterName = recruiter.getRecruiterName();
            studentDto.setRecruiterName(recruiterName);
        }
        if (student != null) {
            return R.success(studentDto);
        }
        return R.error("没有查询到学生信息");
    }


    /**
     * 删除学生
     *
     * @param
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除信息方法")
    public R<String> delete(Integer sid) {
        studentService.removeById(sid);
        return R.success("删除学生成功");
    }
}
